{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ea7c715a",
   "metadata": {},
   "source": [
    "* 日期： 2022-10-19 \\ week09(周三）\n",
    "* 课程：python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27781d58",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "\n",
    "> 1. 列表的方法\n",
    "\n",
    "> 2.while循环\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0208c7b3",
   "metadata": {},
   "source": [
    "## 1.列表的方法\n",
    "\n",
    "> 1.使用tab键查看列表的方法  \n",
    "\n",
    "> 2.使用help（查看方法的使用规则）  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a625d69d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'John']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\", \"Alex\",\"Marry\",\"John\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "101d2007",
   "metadata": {},
   "source": [
    "### 1.1 列表的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d443418c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'John', 'Lihua']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.append(\"Lihua\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "24b447e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 列表是有序的，append加在列表末尾\n",
    "help(names.append)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "59abcc9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Lisa', 'Marry', 'John', 'Lihua']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.insert(2,\"Lisa\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "83151e84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# index(索引值) object\n",
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ffd9334",
   "metadata": {},
   "source": [
    "|values  |\"Jack\"  | \"Alex\"   |  \"Marry\" |   \"John\" |  \n",
    "|-------|------|  -------|  ------- |   ------|\n",
    "| index| 0   |   1   |   2  |   3  |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3f30d7f",
   "metadata": {},
   "source": [
    "### 2. 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "362c5a68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Lisa', 'Marry', 'John', 'Lihua']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.remove(\"Jack\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e00227e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5511632",
   "metadata": {},
   "source": [
    "* 正确的删除方法的流程\n",
    "> 1.查询value是否存在列表当中  \n",
    "> 2.对存在的value进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ac250a9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Wang\" in names "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e9016edd",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"Wang\" in names :\n",
    "    names.remove(\"Wang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d14e9058",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：删除系统 ： \n",
    " ## 1、如果输入的值存在，删除； 2、如果输入的值不存在，提示用户查询无结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f9f239e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Lisa', 'Marry', 'John', 'Lihua']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "282da075",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入要删除的内容：Jack\n",
      "您输入的结果不存在，删除无效\n",
      "['Alex', 'Lisa', 'Marry', 'John', 'Lihua']\n"
     ]
    }
   ],
   "source": [
    "user_input_names = input(\"请输入要删除的内容：\")\n",
    "if user_input_names in names:\n",
    "    names.remove(user_input_names)\n",
    "else:\n",
    "    print(\"您输入的结果不存在，删除无效\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e94dadf0",
   "metadata": {},
   "source": [
    "-----\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "259cd488",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新建立names列表数据\n",
    "names = [\"Jack\", \"Alex\",\"Marry\",\"John\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4632d8dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'John']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c7616ae5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)    # index = -1    pop = 3      # 直接而使用pop(),删除最后一位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9598eb5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'John'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "995e529d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "67a0e0d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "31297c3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a9939552",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)  #查询列表长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ecbb498c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入删除的索引值：2\n"
     ]
    }
   ],
   "source": [
    "pop_index = int(input(\"输入删除的索引值：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e8792a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您输入的值不在index的范围内\n"
     ]
    }
   ],
   "source": [
    "if pop_index<len(names):\n",
    "    names.pop(pop_index)\n",
    "else:\n",
    "    print(\"您输入的值不在index的范围内\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d5beb1",
   "metadata": {},
   "source": [
    "### 3.列表的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "aaf2f223",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_18820/1108101500.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\1\\AppData\\Local\\Temp/ipykernel_18820/1108101500.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    names.\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "names."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "916bb6a6",
   "metadata": {},
   "source": [
    "### 4.列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "08fc6ae1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['Jack','Alex', 'Marry', 'Jan', 'Lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4eb78b7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.clear()\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b0b3888f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1 = ['Jack','Alex', 'Marry', 'Jan', 'Lihua']\n",
    "names_class1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9199501c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "de226b82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['曾洁欣', '王秀梅', '李奥琪']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class2 =[\"曾洁欣\",\"王秀梅\",\"李奥琪\"]\n",
    "names_class2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "291eae35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan', 'Lihua', '曾洁欣', '王秀梅', '李奥琪']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1.extend(names_class2)  # 注意：使用列表的方法的列表进行扩展\n",
    "names_class1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b40b32ca",
   "metadata": {},
   "source": [
    "----\n",
    "* sort()排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9e3e9539",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生 = [230,250,245,255,260,270]   # 注意：使用sort排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "29d52537",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(立定跳远_男生.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4c1c82f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[230, 245, 250, 255, 260, 270]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生.sort()\n",
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "1fa9718f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[230, 245, 250, 255, 260, 270]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生.sort(reverse=False)   #reverse=True是降序  reverse=False是升序\n",
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "742edae9",
   "metadata": {},
   "source": [
    "## 课后作业：\n",
    "> 1.列表的切片和方法的使用  \n",
    "\n",
    ">> 1.课本p67  \n",
    ">> 2.课本p81 练习  \n",
    "\n",
    "> 2.p89：列表,我们已经知道什么"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d8c0f1f",
   "metadata": {},
   "source": [
    "# 2.while循环  \n",
    "\n",
    "* while True 是循环概念中非常重要的一种方式。它可以实现for循环的指定次数，也可以在特定条件下停止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "17d74df9",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'count' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_18820/3456370495.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 死循环\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"你是风儿我是沙\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcount\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m100\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'count' is not defined"
     ]
    }
   ],
   "source": [
    "# 死循环\n",
    "while True:\n",
    "    print(\"你是风儿我是沙\",count)\n",
    "    count = count+1\n",
    "    if count == 100:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d4bfbc5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:66\n",
      "猜一猜（0-100）：76\n"
     ]
    },
    {
     "ename": "SyntaxError",
     "evalue": "'break' outside loop (Temp/ipykernel_18820/900890259.py, line 12)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\1\\AppData\\Local\\Temp/ipykernel_18820/900890259.py\"\u001b[1;36m, line \u001b[1;32m12\u001b[0m\n\u001b[1;33m    break\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m 'break' outside loop\n"
     ]
    }
   ],
   "source": [
    "## 小游戏：猜数字：\n",
    "# 1.hide_card  : 0-100, 大了，小了\n",
    " # 2.guess_digit : 50  \n",
    "hide_card = int(input(\"hide_card:\"))\n",
    "\n",
    "guess_digit = int(input(\"猜一猜（0-100）：\"))\n",
    "\n",
    "#对比：产生两种可能\n",
    "\n",
    "if hide_card == guess_digit:\n",
    "    print(\"你超棒！！\")\n",
    "    break\n",
    "elif guess_digit>hide_card:\n",
    "    print(\"往小猜！加油！\")\n",
    "else:\n",
    "    print(\"往大猜！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b20499ef",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
